package com.heima.behavior.service.impl;
import com.alibaba.fastjson.JSON;
import com.heima.model.mess.UpdateArticleMess.UpdateArticleType;
import java.util.Date;
import java.util.UUID;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.behavior.mapper.ApLikesBehaviorMapper;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.ApLikesBehaviorService;
import com.heima.common.constants.message.HotArticleConstants;
import com.heima.model.behavior.dtos.LikesBehaviorDto;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApLikesBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.mess.UpdateArticleMess;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.threadlocal.AppThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

@Service
public class ApLikesBehaviorServiceImpl extends ServiceImpl<ApLikesBehaviorMapper, ApLikesBehavior> implements ApLikesBehaviorService {
    @Autowired
    private ApBehaviorEntryService  apBehaviorEntryService;
    @Autowired
    private KafkaTemplate<String,String> kafkaTemplate;

    @Override
    public ResponseResult like(LikesBehaviorDto dto) {
        if (dto==null||dto.getArticleId()==null)
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        if (dto.getOperation()==null||(dto.getOperation()!=0&&dto.getOperation()!=1))
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        ApUser user = AppThreadLocalUtils.getUser();
        Integer id = user==null?null:user.getId();
        ApBehaviorEntry apBehaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(id, dto.getEquipmentId());
        if (apBehaviorEntry==null)
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"没有对应的实体对象");
        ApLikesBehavior apLikesBehavior = getOne(Wrappers.<ApLikesBehavior>lambdaQuery().eq(ApLikesBehavior::getArticleId, dto.getArticleId())
                .eq(ApLikesBehavior::getEntryId, apBehaviorEntry.getId())
                .eq(ApLikesBehavior::getType, ApLikesBehavior.Type.ARTICLE));
        if (apLikesBehavior==null){
            apLikesBehavior=new ApLikesBehavior();
            apLikesBehavior.setEntryId(apBehaviorEntry.getId());
            apLikesBehavior.setArticleId(dto.getArticleId());
            apLikesBehavior.setType(dto.getType());
            apLikesBehavior.setOperation(dto.getOperation());
            apLikesBehavior.setCreatedTime(new Date());
            save(apLikesBehavior);
        }else {
            apLikesBehavior.setOperation(dto.getOperation());
            updateById(apLikesBehavior);
        }

        if (apLikesBehavior.getOperation()==0) {
            UpdateArticleMess updateArticleMess = new UpdateArticleMess();
            updateArticleMess.setType(UpdateArticleType.LIKES);
            updateArticleMess.setArticleId(dto.getArticleId());
            updateArticleMess.setAdd(1);
            kafkaTemplate.send(HotArticleConstants.HOT_ARTICLE_SCORE_TOPIC, UUID.randomUUID().toString(), JSON.toJSONString(updateArticleMess));
        }


        return ResponseResult.okResult();
    }
}
